package com.jxau.controller;

import com.jxau.entity.Responses;
import com.jxau.entity.User;
import com.jxau.entity.view.FollowerAndFansNum;
import com.jxau.entity.view.Liker;
import com.jxau.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * @author lts
 * @create 2022-04-22 17:00
 */
@RequestMapping("/user")
@RestController
public class UserController {

    @Autowired
    UserService userService;


    @GetMapping("/login")
    public Responses login(String id, String password, HttpSession session) {
        Integer role = userService.login(id, password, session);
        return Responses.success("登录成功！", role);
    }

    @PostMapping("/register")
    public Responses register(String uid, String password, HttpSession session) {
        Boolean isOkAccount = userService.register(uid, password, session);
        if (isOkAccount) {
            return Responses.success("注册并登录成功！", true);
        } else
            return Responses.error("账号已存在!", false);
    }

    @GetMapping("/getUser")
    public Responses getUser(HttpSession session) {
        return Responses.success("客户端用户查询成功", session.getAttribute("user"));
    }

    @GetMapping("/queryUserHeadById")
    public Responses queryUserHeadById(String uid) {
        String headPhoto = userService.queryHeadPhotoById(uid);
        return Responses.success("头像查询成功", headPhoto);
    }

    @DeleteMapping("/doUnLike")
    public Responses doUnLike(Integer articleId, String uid) {
        userService.doUnLike(articleId, uid);
        return Responses.success("取消点赞操作成功");
    }

    @PutMapping("/doLike")
    public Responses doLike(Integer articleId, String uid) {
        userService.doLike(articleId, uid);
        return Responses.success("点赞成功！");
    }

    @PutMapping("/doCollect")
    public Responses doCollect(Integer articleId, String uid) {
        userService.doCollect(articleId, uid);
        return Responses.success("收藏成功！");
    }

    @DeleteMapping("/doUnCollect")
    public Responses doUnCollect(Integer articleId, String uid) {
        userService.doUnCollect(articleId, uid);
        return Responses.success("取消收藏成功！");
    }

    @DeleteMapping("/doUnLikeForComment")
    public Responses doUnLikeForComment(Integer commentId, String uid) {
        userService.doUnLikeForComment(commentId, uid);
        return Responses.success("评论取消点赞操作成功");
    }

    @PutMapping("/doLikeForComment")
    public Responses doLikeForComment(Integer commentId, String uid) {
        userService.doLikeForComment(commentId, uid);
        return Responses.success("评论点赞成功！");
    }

    @GetMapping("/queryUserInfoById")
    public Responses queryUserInfoById(String uid) {
        User user = userService.queryUserInfoById(uid);
        return Responses.success("查询指定用户信息成功！", user);
    }

    @PostMapping("/editHeadPhoto")
    public Responses editHeadPhoto(String uid, MultipartFile file) {
        userService.updateHeadPhoto(uid, file);
        return Responses.success("修改头像成功！");
    }

    @PostMapping("/editUserInfo")
    public Responses editUserInfo(User user) {
        // 字符串长度和邮箱格式检查
        Pattern pattern = Pattern.compile("^[A-Za-z0-9\\u4e00-\\u9fa5]+@[a-zA-Z0-9_-]+(.[a-zA-Z0-9_-]+)+$");
        if(user.getUsername().length() >8||user.getSignature().length()>20||
                (!"男".equals(user.getGender())&&!"女".equals(user.getGender()))||
                user.getSchool().length()>20||pattern.matcher(user.getEmail()).matches()){
            return Responses.error("邮箱格式不对或字符串太长");
        }
        // 修改用户信息
        userService.updateUserInfo(user);
        return Responses.success("修改用户信息成功！");
    }

    @GetMapping("/queryFollowers")
    public Responses queryFollowers(String uid) {
        ArrayList<User> follows = userService.queryFollowers(uid);
        return Responses.success("查询我的关注成功！", follows);
    }

    @GetMapping("/queryFans")
    public Responses queryFans(String uid) {
        ArrayList<User> myFans = userService.queryFans(uid);
        return Responses.success("查询我的粉丝成功！", myFans);
    }

    @GetMapping("/queryFollowerAndFansNum")
    public Responses queryFollowerAndFansNum(String uid) {
        FollowerAndFansNum followerAndFansNum = userService.queryFollowerAndFansNum(uid);
        return Responses.success("查询关注数和粉丝数成功", followerAndFansNum);
    }

    @GetMapping("/queryIsFollow")
    public Responses queryIsFollow(String uid, String otherUid) {
        Boolean isFollow = userService.queryIsFollow(uid, otherUid);
        return Responses.success("查询是否关注成功", isFollow);
    }

    @PostMapping("/followUser")
    public Responses followUser(String uid, String otherUid) {
        userService.followUser(uid, otherUid);
        return Responses.success("关注成功");
    }

    @DeleteMapping("/cancelFollow")
    public Responses cancelFollow(String uid, String otherUid) {
        userService.cancelFollow(uid, otherUid);
        return Responses.success("取消关注成功");
    }

    @GetMapping("/getNewCommentAndLikeNum")
    public Responses getNewCommentAndLikeNum(String uid) {
        Map<String, Integer> result = userService.getNewCommentAndLikeNum(uid);
        return Responses.success("获取新评论和点赞数成功！", result);
    }

    @GetMapping("/getNewLiker")
    public Responses getNewLiker(String uid, @RequestParam(defaultValue = "1") int nowPage) {
        Map<String,Object> map = userService.getNewLiker(uid,nowPage);
        return Responses.success("获取用户点赞数据成功！", map.get("likerList"), (Integer) map.get("pageNum"));
    }

    @PutMapping("/cleanRedPointOfLike")
    public Responses cleanRedPointOfLike(String uid) {
        userService.cleanRedPointOfLike(uid);
        return Responses.success("清除点赞红点成功！");
    }
//--------------------------------------------------------------------------

    @RequestMapping("/addUser")
    public int addUser(@RequestBody Map userInfo) {
        //将map的数据取出封装到user中
        String id = String.valueOf(userInfo.get("id"));
        String username = String.valueOf(userInfo.get("username"));
        String password = String.valueOf(userInfo.get("password"));
        String gender = String.valueOf(userInfo.get("gender"));
        String school = String.valueOf(userInfo.get("school"));
        String email = String.valueOf(userInfo.get("email"));
        String signature = String.valueOf(userInfo.get("signature"));
//        String headPhoto = String.valueOf(userInfo.get("headPhoto"));
        User user = new User(id, username, password, null, 0, gender, school, email, null, signature);
        return userService.addUser(user);
    }

    @RequestMapping("/userList")
    @ResponseBody
    public List<User> showUserList() {
        List<User> users = userService.show();
        return users;
    }

    @RequestMapping("/findById")
    public User findUserById(@RequestBody Map map) {
        User user = userService.findUserById((String) map.get("id"));
        return user;
    }

    @RequestMapping("/findUser")
    @ResponseBody
    public List<User> findUser(String name) {
        List<User> users = userService.findUserByName(name);
        return users;
    }

    @CrossOrigin
    @RequestMapping("/updateUser")
    @ResponseBody
    public int updateUser(@RequestBody User user) {
        int result = userService.updateUser(user);
        return result;
    }

    @RequestMapping("/deleteUser")
    @CrossOrigin
    @ResponseBody
    public int deleteUser(String id) {
        int result = userService.deleteUser(id);
        return result;
    }

    @RequestMapping("/page")
    public List<User> getPages(int pageNum, int pageSize) {
        List<User> users = userService.getPages(pageNum, pageSize);
        return users;
    }

    /**
     * 获取分页条的相关信息
     *
     * @return
     */
    @RequestMapping("/pageData")
    public PageInfo<User> getPagesData() {
        return userService.getPagesData();
    }
}
